package plugins.lxfa.mainMenuBottom
{
    import caurina.transitions.*;
    
    import core.manager.*;
    import core.manager.sceneManager.*;
    import core.manager.scriptManager.*;
    
    import flash.events.*;
    
    import memory.*;
    
    import mx.core.*;
    
    import view.fl2mx.Fl2Mx;
    import view.player.*;

    public class MainMenuBottom extends UIComponent
    {
        private const focusMinRange:int = 250;
        private const cameraRotateSpeed:int = 8;
        private var bottomSign:MainMenuBottomSign;
        private var focusSpeed:int = 10;
        private var currentSceneId:int = -1;
        private var tempFocus:int = 0;
        private var bottom:MainMenuBottomSwc;
        private const focusMaxRange:int = 450;

        public function MainMenuBottom()
        {
            initbottom();
            return;
        }// end function

        private function rightEnterFrame(e:Event) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [0, cameraRotateSpeed]);
            }
            return;
        }// end function

        private function cameraDownMouseDownEvent(e:MouseEvent) : void
        {
            this.addEventListener(Event.ENTER_FRAME, downEnterFrame);
            return;
        }// end function

        private function cameraUpMouseDownEvent(e:MouseEvent) : void
        {
            this.addEventListener(Event.ENTER_FRAME, upEnterFrame);
            return;
        }// end function

        private function addAD() : void
        {
        	var ad:Ad=new Ad();
        	Application.application.addChild(Fl2Mx.fl2Mx(ad));
        }// end function

        private function cameraRightMouseDownEvent(e:MouseEvent) : void
        {
            this.addEventListener(Event.ENTER_FRAME, rightEnterFrame);
            return;
        }// end function

        private function cameraLeftMouseUpEvent(e:MouseEvent) : void
        {
            MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, leftEnterFrame);
            return;
        }// end function

        private function on_jingxiuhuazhang_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(3);
            return;
        }// end function

        private function changeColor(e:SceneChangeEvent) : void
        {
            if (e.id == 0)
            {
                bottomSign.x = bottom.lvsejiayuan.x;
                bottomSign.y = bottom.lvsejiayuan.y;
            }
            else if (e.id == 1)
            {
                bottomSign.x = bottom.lansemengxiang.x;
                bottomSign.y = bottom.lansemengxiang.y;
            }
            else if (e.id == 2)
            {
                bottomSign.x = bottom.meiguantianxia.x;
                bottomSign.y = bottom.meiguantianxia.y;
            }
            else if (e.id == 3)
            {
                bottomSign.x = bottom.jingxiuhuazhang.x;
                bottomSign.y = bottom.jingxiuhuazhang.y;
            }
            else if (e.id == 4)
            {
                bottomSign.x = bottom.shengshihexie.x;
                bottomSign.y = bottom.shengshihexie.y;
            }
            else if (e.id == 5)
            {
                bottomSign.x = bottom.zonghengsihai.x;
                bottomSign.y = bottom.zonghengsihai.y;
            }
            else if (e.id == 6)
            {
                bottomSign.x = bottom.yangmengbagui.x;
                bottomSign.y = bottom.yangmengbagui.y;
            }else if(e.id==12)
            {
            	bottomSign.x=10000;
            }
            return;
        }// end function

        private function on_shengshihexie_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(4);
            return;
        }// end function

        private function on_lvsejiayuan_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(0);
            return;
        }// end function

        private function stageKeyEvent(e:KeyboardEvent) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                if (e.keyCode == 37)
                {
                    MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [0, -cameraRotateSpeed]);
                }
                else if (e.keyCode == 38)
                {
                    MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [cameraRotateSpeed, 0]);
                }
                else if (e.keyCode == 39)
                {
                    MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [0, cameraRotateSpeed]);
                }
                else if (e.keyCode == 40)
                {
                    MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [-cameraRotateSpeed, 0]);
                }
            }
            return;
        }// end function

        private function helpClickEvent(e:MouseEvent) : void
        {
            ScriptManager.getInstance().runScriptByName(ScriptName.SHOW_SWF, ["swf/help.swf", false, true]);
            return;
        }// end function

        private function jieshaoClickEvent(e:MouseEvent) : void
        {
            ScriptManager.getInstance().runScriptByName(ScriptName.SHOW_NORMAL_WINDOW, [60]);
            return;
        }// end function

        private function cameraLeftMouseDownEvent(e:MouseEvent) : void
        {
            this.addEventListener(Event.ENTER_FRAME, leftEnterFrame);
            return;
        }// end function

        private function cameraUpMouseUpEvent(e:MouseEvent) : void
        {
            MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, upEnterFrame);
            return;
        }// end function

        private function leftEnterFrame(e:Event) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [0, -cameraRotateSpeed]);
            }
            return;
        }// end function

        private function cameraNotAddClickEvent(e:MouseEvent) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                tempFocus = MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA).focus - focusSpeed;
                if (tempFocus > focusMinRange)
                {
                    MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA).focus = MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA).focus - focusSpeed;
                }
            }
            return;
        }// end function

        private function cameraAddClickEvent(e:MouseEvent) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                tempFocus = MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA).focus + focusSpeed;
                if (tempFocus < focusMaxRange)
                {
                    MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA).focus = MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA).focus + focusSpeed;
                }
            }
            return;
        }// end function

        private function cameraDownMouseUpEvent(e:MouseEvent) : void
        {
            MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, downEnterFrame);
            return;
        }// end function

        private function initEvent() : void
        {
            bottom.cameraAdd.addEventListener(MouseEvent.CLICK, cameraAddClickEvent);
            bottom.cameraNotAdd.addEventListener(MouseEvent.CLICK, cameraNotAddClickEvent);
            bottom.help.addEventListener(MouseEvent.CLICK, helpClickEvent);
            bottom.jieshao.buttonMode = true;
            bottom.jieshao.addEventListener(MouseEvent.CLICK, jieshaoClickEvent);
            bottom.cameraUp.addEventListener(MouseEvent.MOUSE_DOWN, cameraUpMouseDownEvent);
            bottom.cameraUp.addEventListener(MouseEvent.MOUSE_UP, cameraUpMouseUpEvent);
            bottom.cameraDown.addEventListener(MouseEvent.MOUSE_DOWN, cameraDownMouseDownEvent);
            bottom.cameraDown.addEventListener(MouseEvent.MOUSE_UP, cameraDownMouseUpEvent);
            bottom.cameraLeft.addEventListener(MouseEvent.MOUSE_DOWN, cameraLeftMouseDownEvent);
            bottom.cameraLeft.addEventListener(MouseEvent.MOUSE_UP, cameraLeftMouseUpEvent);
            bottom.cameraRight.addEventListener(MouseEvent.MOUSE_DOWN, cameraRightMouseDownEvent);
            bottom.cameraRight.addEventListener(MouseEvent.MOUSE_UP, cameraRightMouseUpEvent);
            Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN, stageKeyEvent);
            bottom.lvsejiayuan.addEventListener(MouseEvent.CLICK, on_lvsejiayuan_click);
            bottom.lansemengxiang.addEventListener(MouseEvent.CLICK, on_lansemengxiang_click);
            bottom.meiguantianxia.addEventListener(MouseEvent.CLICK, on_meiguantianxia_click);
            bottom.jingxiuhuazhang.addEventListener(MouseEvent.CLICK, on_jingxiuhuazhang_click);
            bottom.shengshihexie.addEventListener(MouseEvent.CLICK, on_shengshihexie_click);
            bottom.zonghengsihai.addEventListener(MouseEvent.CLICK, on_zonghengsihai_click);
            bottom.yangmengbagui.addEventListener(MouseEvent.CLICK, on_yangmengbagui_click);
            bottomSign = new MainMenuBottomSign();
            bottom.addChild(bottomSign);
            bottomSign.y = 500;
            SceneManager.getInstance().addEventListener(SceneChangeEvent.COMPLETE, changeColor);
            return;
        }// end function

        private function upEnterFrame(e:Event) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [cameraRotateSpeed, 0]);
            }
            return;
        }// end function

        private function on_meiguantianxia_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(2);
            return;
        }// end function

        private function on_yangmengbagui_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(6);
            return;
        }// end function

        private function on_lansemengxiang_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(1);
            return;
        }// end function

        private function downEnterFrame(e:Event) : void
        {
            if (MainSystem.getInstance().runAPIDirectDirectly(ScriptName.GET_CAMERA) != null)
            {
                MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [-cameraRotateSpeed, 0]);
            }
            return;
        }// end function

        private function cameraRightMouseUpEvent(e:MouseEvent) : void
        {
            MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, rightEnterFrame);
            return;
        }// end function

        private function initbottom() : void
        {
            if (bottom == null)
            {
                bottom = new MainMenuBottomSwc();
                initEvent();
            }
            this.addChild(bottom);
            bottom.alpha = 0;
            Tweener.addTween(bottom, {alpha:1, time:3});
//            addAD();
            return;
        }// end function

        private function on_zonghengsihai_click(e:MouseEvent) : void
        {
            SceneManager.getInstance().gotoScene(5);
            return;
        }// end function

    }
}
